package gw.com.android.ui.home;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigType;
import gw.com.android.presenter.AdsData;
import gw.com.android.utils.NetUtil;
import www.com.library.model.DataItemDetail;
import www.com.library.util.DeviceUtil;

/**
 * 首页 新手 福利广告
 *
 * @author jett
 * @since 2018-11-08.
 */
public class HomeWelfareView extends RelativeLayout {

    private View viewEmpty, viewStub;
    private TextView tvTitle;
    private LinearLayout llList;

    public HomeWelfareView(Context context) {
        super(context);
    }

    public HomeWelfareView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public HomeWelfareView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        viewEmpty = findViewById(R.id.welfare_empty);
        viewStub = findViewById(R.id.welfare_stub);
        tvTitle = findViewById(R.id.welfare_title);
        llList = findViewById(R.id.welfare_list);
    }

    public void onRefresh(final Activity activity, AdsData adsData) {
        if (llList == null) {
            return;
        }
        viewStub.setVisibility(GONE);
        llList.removeAllViews();
        String title = "新手福利";
        if (adsData == null || adsData.adPics == null || adsData.adPics.isEmpty()) {//没有缓存，就显示占位图
            tvTitle.setText(title);
            viewEmpty.setVisibility(VISIBLE);

            if (!NetUtil.isNetworkAvailable(activity)) {//如果网络不可用
                //就显示占位图
            } else {//如果可用，就让view消失
                setVisibility(GONE);
            }
        } else {
            setVisibility(VISIBLE);
            viewEmpty.setVisibility(GONE);
            String text = TextUtils.isEmpty(adsData.title) ? adsData.content : adsData.title;
            text = TextUtils.isEmpty(text) ? title : text;
            tvTitle.setText(text);
            int screenWidth = DeviceUtil.instance().getScreenPixelsWidth(getContext());
            int imageWidth = screenWidth - DeviceUtil.instance().dip2px(30, getContext());
            int imageHeight = (int) (210 * 1.0F / 1035 * imageWidth); /// 图片宽高比为 1035:210
            for (AdsData.PicData picData : adsData.adPics) {
                final String adTitle = picData.title;
                final String url = picData.url;
                final String link = picData.link;
                ImageView imageView = new ImageView(getContext());
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, imageHeight);
                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                imageView.setLayoutParams(params);
                Glide.with(getContext())
                        .load(url)
                        .placeholder(R.mipmap.a_default_adslip)
                        .error(R.mipmap.a_default_adslip)
                        .dontAnimate()//防止设置placeholder导致第一次不显示网络图片,只显示默认图片的问题
                        .into(imageView);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        DataItemDetail detail = new DataItemDetail();
                        detail.setStringValue(ConfigType.CONFIG_TYPE_TYPE_URL_TAG, link);
                        detail.setStringValue(ConfigType.CONFIG_TYPE_TITLE_TAG, adTitle);
                        ActivityManager.showWebPageActivity(activity, detail, AppMain.getAppString(R.string.btn_back));
                    }
                });
                llList.addView(imageView);
            }
        }
    }

}
